package vip.xiaonuo.vein.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.pojo.response.ErrorResponseData;
import vip.xiaonuo.facedoor.iservice.IDeviceSetService;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.deviceset.entity.DwDeviceSet;
import vip.xiaonuo.facedoor.modular.deviceset.param.DwDeviceSetParam;
import vip.xiaonuo.facedoor.rule.RuleConstant;
import vip.xiaonuo.vein.controller.vo.Restart;
import vip.xiaonuo.vein.model.ReqUtil;
import vip.xiaonuo.vein.modular.veindeviceevent.entity.VeinDeviceEvent;
import vip.xiaonuo.vein.modular.veindeviceevent.enums.VeinDeviceEventPersonTypeEnum;
import vip.xiaonuo.vein.modular.veindeviceevent.enums.VeinDeviceEventStatusEnum;
import vip.xiaonuo.vein.modular.veindeviceevent.enums.VeinDeviceEventSysTypeEnum;
import vip.xiaonuo.vein.modular.veindeviceevent.service.VeinDeviceEventService;
import vip.xiaonuo.vein.websocket.WebSocket;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.InetAddress;

@Service(value = RuleConstant.VEIN + RuleConstant.DEVICE_SET)
public class VeinDeviceSetService implements IDeviceSetService {

    @Resource
    private DeviceBaseService deviceBaseService;

    @Resource
    private ReqUtil reqUtil;

    @Resource
    private WebSocket webSocket;

    @Resource
    private VeinDeviceEventService eventService;

    @Override
    public void saveOrUpdate(DwDeviceSetParam dwDeviceSetParam) {

    }

    @Override
    public void restart(String deviceId) {
        DeviceBase deviceBase = deviceBaseService.getById(deviceId);

        // 调用接口远程重启
        if (deviceBase == null) {
            throw new ServiceException(500, "设备不存在！");
        }

        String status = deviceBase.getDeviceState();
        if (!StringUtils.equals("1",status) ){
            throw new ServiceException(500,"设备不在线! ");
        }

        boolean isReachable = false;
        try {
            isReachable = InetAddress.getByName(deviceBase.getAddress()).isReachable(5000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(!isReachable){
            deviceBase.setDeviceState("2");
            deviceBaseService.updateById(deviceBase);
            throw new ServiceException(500,"访问设备失败,请检查网络连接! ");
        }

        String deviceCode = deviceBase.getRegistrationCode();
        Restart restart = new Restart();
        String serialNo = reqUtil.getReqSeriNo(deviceCode, restart);
        String data = serialNo.concat(JSON.toJSONString(restart));
        webSocket.sendOneMessage(deviceCode,data);

        VeinDeviceEvent veinDeviceEvent = new VeinDeviceEvent()
                .setBody(deviceBase.getId()+"")
                .setRequest(data)
                .setStatus(VeinDeviceEventStatusEnum.EXECUTE_SUCCESS.getCode())
                .setSerialNumber(serialNo)
                .setDeviceId(deviceBase.getId());
        veinDeviceEvent.setSysType(VeinDeviceEventSysTypeEnum.DEVICE.getCode());
        veinDeviceEvent.setType(VeinDeviceEventPersonTypeEnum.RESTART_DEVICE.getCode());
        eventService.save(veinDeviceEvent);
    }
}
